<!-- 文件审核 -->
<script lang="ts" setup>
import { reactive } from 'vue'
import type { IDbAction, IDbSearchConfig, IDbTable, ITagData } from '@/types/global'
import useNotic from '@/hooks/useNotic'
import * as fileApi from '@/api/file'
import { FILE_CHECK_STATUS, ENUM_FILE_CHECK_STATUS } from './config/config'
import { ENUM_BTN_AUTH_FILE } from '@/config/btn'

// 搜索栏配置
const searchConfig = reactive<IDbSearchConfig[]>([{ label: '配置标题', prop: 'configTitle', type: 'input' }])

// 表格数据
const tableData = reactive<IDbTable>({
  columns: [
    { label: '序号', prop: '', type: 'index', width: '60px' },
    { label: '配置标题', prop: 'configTitle', minWidth: '100px' },
    { label: '应用名称', prop: 'appName', minWidth: '100px' },
    { label: '渠道名称', prop: 'channelName', minWidth: '100px' },
    {
      label: '状态',
      prop: 'status',
      type: 'tag',
      width: '100px',
      tagFunc: (scoped) => {
        const curStatus = FILE_CHECK_STATUS.find((item) => item.value == scoped.row.status)
        const params: ITagData = { label: curStatus?.label || '', type: curStatus?.type || '' }
        return params
      }
    },
    { label: '操作', prop: 'handle', fixed: 'right', type: 'slot', width: '150px' }
  ],
  data: [],
  total: 0,
  search: {
    pageNum: 1,
    pageSize: 20,
    status: ENUM_FILE_CHECK_STATUS['待审核'],
    configTitle: ''
  }
})

getDataList()

/**
 * 获取表格数据
 */
function getDataList() {
  fileApi.getFileCheckByPage(tableData.search).then((res) => {
    if (res.success) {
      tableData.total = res.data?.total || 0
      tableData.data = res.data?.rows || []
    }
  })
}
/**
 * 点击通过/驳回
 */
function checkClick(scoped: any, isPass: boolean) {
  const des = isPass ? '通过' : '驳回'
  const status = isPass ? ENUM_FILE_CHECK_STATUS['审核通过'] : ENUM_FILE_CHECK_STATUS['已驳回']
  useNotic().confirm(`您确定要${des}<${scoped.row.configTitle}>的审核吗？`, (flag) => {
    if (flag) {
      fileApi.editFileCheck({ id: scoped.row.id, status }).then((res) => {
        if (res.success) {
          useNotic().success(`${des}成功`)
          getDataList()
        }
      })
    }
  })
}
/**
 * 设置表格按钮
 */
function setBtnList(scoped: any) {
  const list: IDbAction[] = [
    { label: '通过', type: 'primary', auth: ENUM_BTN_AUTH_FILE['文件审核:通过'], onClick: () => checkClick(scoped, true) },
    { label: '驳回', type: 'danger', auth: ENUM_BTN_AUTH_FILE['文件审核:驳回'], onClick: () => checkClick(scoped, true) }
  ]
  return list
}
</script>

<template>
  <dui-page :search-config="searchConfig" :search-data="tableData.search" @search="getDataList">
    <template #default>
      <dui-table
        :data="tableData.data"
        :columns="tableData.columns"
        :total="tableData.total"
        :search="tableData.search"
        @search="getDataList"
      >
        <template #handleSlot="scoped">
          <dui-action :list="setBtnList(scoped)" />
        </template>
      </dui-table>
    </template>
  </dui-page>
</template>
